package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.component.design.Button;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IVariableDao;
import cn.edu.sjtu.se.dslab.project.Variable;

@Repository("variableDao")
public class VariableDaoImpl implements IVariableDao{

		@Resource(name = "baseDao")
		private IBaseDao<Variable> baseDao;
		
		@Override
		public Variable queryById(long id) {
			return baseDao.queryById(Variable.class, id);
		}

		@Override
		public List<Variable> queryByList() {
			return baseDao.queryList(Variable.class.getName());
		}

		@Override
		public void save(Variable variable) {
			baseDao.save(variable);
		}

		@Override
		public Variable update(Variable variable) {
			return baseDao.update(variable);
		}
		
		//only the project
		@Override
		public List<Variable> queryByProject(long projectId) {
			StringBuilder sql = new StringBuilder();
			sql.append("from Variable where project_id='");
			sql.append(projectId).append("'");
			return baseDao.query(sql.toString());
		}

		@Override
		public void delete(Variable variable) {
			baseDao.delete(variable);
		}
		
		//not only the project
		@Override
		public List<Variable> queryValidByProject(long projectId) {
			StringBuilder sql = new StringBuilder();
			sql.append("from Variable where project_id='");
			sql.append(projectId).append("' or project_id is null");
			return baseDao.query(sql.toString());
		}
		
		@Override
		public Variable queryByProjectAndName(long projectId, String name) {
			StringBuilder sql = new StringBuilder();
			sql.append("from Variable where project_id='");
			sql.append(projectId).append("' and name = '").append(name).append("'");
			return baseDao.queryObject(sql.toString());
		}
}
